package cn.edu.zafu.bigdata.xct.FileOperation;

import cn.edu.zafu.bigdata.x.wupeng.g.ReverseThread;

import java.io.*;

public class RhymingWords {
    public static void main(String[] args) throws IOException{
        FileReader words = new FileReader("words.txt");

        Reader rhymingWords = reverse(sort(reverse(words)));

        BufferedReader in = new BufferedReader(rhymingWords);
        String input;
        while((input = in.readLine()) != null);
            System.out.println(input);
        in.close();
    }

    public static Reader reverse(Reader source) throws IOException{
        BufferedReader in = new BufferedReader(source);
        PipedWriter pipedOut = new PipedWriter();
        PipedReader pipedIn = new PipedReader(pipedOut);
        PrintWriter out = new PrintWriter(pipedOut);
        new ReverseThread(out,in).start();
        return pipedIn;
    }

    public static Reader sort(Reader source) throws IOException{
        BufferedReader in = new BufferedReader(source);
        PipedWriter pipedOut = new PipedWriter();
        PipedReader pipedIn = new PipedReader(pipedOut);
        PrintWriter out = new PrintWriter(pipedOut);
        new SortThread(out,in).start();
        return pipedIn;
    }
}
